
import cv2
import math

def Rad2ONVIF(rad):
    '''
        弧度转ONVIF控制量
    '''
    return rad/math.pi

def ONVIF2Rad(val):
    '''
        ONVIF转弧度
    '''
    return val*math.pi

def XYZ2ONVIFParams(x,y,z,zoom_rate=10.0):
    '''
        从XYZ到ONVIF数据
    '''
    distance=math.sqrt(x*x+y*y+z*z)
    pan=math.atan2(y,x)
    tilt=math.asin(z/distance)
    zoom=distance/zoom_rate
    if zoom<1:zoom=1
    return pan,tilt,zoom

class Tracker:
    '''
        目标追踪
    '''

    def __init__(self,frame,roi):
        '''
            初始化追踪器
        '''
        tracker = cv2.TrackerCSRT_create()
        ok = tracker.init(frame, roi)

    def UpdateTrack(self,frame):
        '''
            更新数据并追踪
        '''
        ok, bbox = self.tracker.update(frame)
        if ok:
            return True,bbox
        else:
            return False,None